<template>
  <div class="container">
    <div class="form-list">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field label="所选行业" v-model="industry" readonly />
          <van-field
            v-model="phone"
            type="phone"
            name="手机号"
            label="手机号后四位"
            placeholder="请输入手机号后四位"
            :rules="[{ required: true, message: '请填写手机号' }]"
            required
       
          />
          <van-field
            v-model="name"
            name="姓名"
            label="姓名"
            placeholder="请输入姓名"
            :rules="[{ required: true, message: '请填写用户名' }]"
            required
          />

          <!-- <van-field
            v-model="village"
            name="所属村"
            label="所属村"
            placeholder="请输入您现在的所属村"
            :rules="[{ required: true, message: '请输入您现在的所属村' }]"
            required
          /> -->
          <van-field
            readonly
            clickable
            name="所属村"
            :value="village"
            label="所属村"
            placeholder="请输入您现在的所属"
            :rules="[{ required: true, message: '请输入您现在的所属村' }]"
            @click="showPicker = true"
            required
          />
          <van-popup v-model="showPicker" position="bottom">
            <van-picker
              show-toolbar
              :columns="columns"
              @confirm="onConfirm"
              @cancel="showPicker = false"
            />
          </van-popup>
          <van-field
            v-model="dept"
            name="所在单位"
            label="所在单位"
            placeholder="请输入您现在所在单位"
            :rules="[{ required: true, message: '请输入您现在所在单位' }]"
            required
          />
        </van-cell-group>
        <div style="margin: 20px">
          <van-button round block type="primary" native-type="submit">
            下一步
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { getAxios2 } from "@/utils/request2";
import { Picker } from "vant";
export default {
  components: {
    vanPicker: Picker,
  },
  data() {
    return {
      industry: "", // 所选行业
      itemCode: "",
      info: "",
      name: "",
      phone: "",
      village: "",
      dept: "",
      value: "",
      columns: [
        "黄洞村",
        "凤岗居委",
        "雁田村",
        "官井头村",
        "油甘埔村",
        "凤德岭村",
        "塘沥村",
        "三联村",
        "五联村",
        "天堂围村",
        "竹塘村",
        "竹尾田村",
        "金凤凰工业区",
        "大龙工业区",
        "宏盈工业区",
        "玉泉工业区",
      ],
      showPicker: false,
    };
  },
  mounted() {
    // console.log(this.$route.query.item)
    this.industry = this.$route.query.title;
    this.itemCode = this.$route.query.itemCode;
    this.flag = this.$route.query.flag;
    localStorage.removeItem("userId");
  },
  methods: {
    onConfirm(value) {
      this.village = value;
      this.showPicker = false;
    },
    onSubmit() {
      //验证名字的正则表达式
      const regName = /^[\u4E00-\u9FA5]{2,4}$/;
      // 验证手机号的正则表达式
      // var regPhone = /^1[3-9]\d{9}$/;
      var regPhone = /^[0-9]{4}$/;
      if (!regName.test(this.name)) {
        return this.$toast("请输入合法的姓名！");
      }
      if (!regPhone.test(this.phone)) {
        // return this.$toast("请输入合法的手机号！");
        return this.$toast("请输入四位的号码！");
      }
      const userdata = {
        name: this.name,
        telNo: this.phone,
        village: this.village,
        workPlace: this.dept,
      };
      this.saveStudyMsg(userdata);
      // localStorage.setItem("userId", this.phone);
    },
    // 保存学习用户信息
    async saveStudyMsg(data) {
      let axios2 = await getAxios2();
      // const res = await this.$axios.post('http://120.79.234.223:8558/app/study/saveMember',data)

      const res = await axios2.post(
        // "https://fgbm.zatsafety.com/app/study/saveMember",
        "http://192.168.138.22:8558/app/study/saveMember",
        data
      );
      console.log(res);
      if (res.msg === "success") {
        localStorage.setItem("FgbmName", this.name);
        localStorage.setItem("userId", res.data);
        let path;
        if (this.flag) {
          path = "/fgbm/info";
        } else {
          path = "/fgbm/video";
        }
        this.$router.push({
          path: path,
          query: { industryType: this.itemCode },
        });
      }
    },
    async findUserInfo() {
      var self = this;
      let axios2 = await getAxios2();
      let params = new FormData(); //创建form对象
      params.append("params[memberId]", self.phone);
      const res = await axios2.post(
        "https://fgbm.zatsafety.com/app/study/memberList",
        params
      );
      let data = res.data[0];
      if (data) {
        self.name = data.memberName;
        self.village = data.village;
        self.dept = data.workPlace;
      } else {
        self.name = "";
        self.village = "";
        self.dept = "";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  .form-list {
    width: 90%;
    margin: 20px auto;
    height: 80vh;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 5px #ccc;
  }
}
</style>